package com.biostime.springdraw.modules.sd.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.springdraw.common.config.Global;
import com.biostime.springdraw.common.persistence.Page;
import com.biostime.springdraw.common.utils.StringUtils;
import com.biostime.springdraw.common.web.BaseController;
import com.biostime.springdraw.modules.sd.entity.Awardtype;
import com.biostime.springdraw.modules.sd.service.AwardtypeService;
import com.biostime.springdraw.modules.sys.entity.User;

/**
 * 奖品类型表Controller
 * @author chenweicheng
 * @version 2016-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/awardtype")
public class AwardtypeController extends BaseController {

	@Autowired
	private AwardtypeService AwardtypeService;
	
	@ModelAttribute
	public Awardtype get(@RequestParam(required=false) String id) {
		Awardtype entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = AwardtypeService.get(id);
		}
		if (entity == null){
			entity = new Awardtype();
		}
		return entity;
	}
	
	@RequiresPermissions("sd:awardtype:view")
	@RequestMapping(value = {"list", ""})
	public String list(Awardtype Awardtype, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Awardtype> page = AwardtypeService.findPage(new Page<Awardtype>(request, response), Awardtype); 
		model.addAttribute("page", page);
		return "modules/sd/AwardtypeList";
	}

	@RequiresPermissions("sd:awardtype:view")
	@RequestMapping(value = "form")
	public String form(Awardtype Awardtype, Model model) {
		model.addAttribute("Awardtype", Awardtype);
		return "modules/sd/AwardtypeForm";
	}

	@RequiresPermissions("sd:awardtype:edit")
	@RequestMapping(value = "save")
	public String save(Awardtype Awardtype, Model model, RedirectAttributes redirectAttributes) {
		Awardtype.setCreateBy(new User("0"));
		Awardtype.setCreateDate(new Date());
		Awardtype.setUpdateBy(new User("0"));
		Awardtype.setUpdateDate(new Date());
		if (!beanValidator(model, Awardtype)){
			return form(Awardtype, model);
		}
		AwardtypeService.save(Awardtype);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/sd/awardtype/?repage";
	}
	
	@RequiresPermissions("sd:awardtype:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Awardtype Awardtype, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				AwardtypeService.delete(new Awardtype(id));
			}
		}else{
			AwardtypeService.delete(Awardtype);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/sd/awardtype/?repage";
	}

}